﻿
@{
    ViewData["Title"] = "AdminSql";
    Layout = "~/Views/Shared/_ManageLayout.cshtml";
}
@section css{
    <style>
        .layui-table-cell {
            line-height: 25px;
        }
    </style>
}
<div class="layui-fluid" style="padding-bottom: 75px;">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form-item layui-row">
                <div class="layui-inline layui-col-md12">
                    <label class="layui-form-label layui-form-required">执行Sql:</label>
                    <div class="layui-input-block">
                        <textarea id="executeSql" name="executeSql" placeholder="请输入栏目内容" class="layui-textarea"
                                  lay-verType="tips" style="height:200px"></textarea>
                    </div>
                </div>
                <div class="layui-inline layui-col-md12">
                    <a class="layui-btn" lay-filter="formSelect" lay-submit>执行查询操作</a>
                    <a class="layui-btn" lay-filter="formUpdate" lay-submit>执行非查询操作</a>
                </div>
            </div>

        </div>
    </div>
    <div class="layui-card">
        <div class="layui-card-header">执行结果</div>
        <div class="layui-card-body">
            <span id="myText"></span>
            <table id="tableTbBas" lay-filter="tableTbBas"></table>
        </div>
    </div>
</div>

<script>
    layui.use(['admin', 'layer', 'form', 'table', 'laydate', 'laytpl', 'upload'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var laydate = layui.laydate;
        var admin = layui.admin;
        var laytpl = layui.laytpl;
        var upload = layui.upload;
        // 搜索按钮点击事件
        form.on('submit(formSelect)', function (data) {
            layer.load(2);
            $("#myText").text("");
            var executeSql = $("[name=executeSql]").val();
            $.ajax({
                url: "/api/AdminSql",
                type: 'post',
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify({ executeSql: executeSql }),
                dataType: "json",
                complete: function (obj) {
                    layer.closeAll('loading');
                },
                success: function (res) {
                    if (res.success) {
                        table.render({
                            elem: '#tableTbBas',
                            data: res.data.table,
                            page: false,
                            cellMinWidth: 100,
                            cols: [res.data.cols],
                            limit: 9999,
                        });
                    } else {
                        $("#myText").text(res.message);
                        table.render({
                            elem: '#tableTbBas',
                            data: [],
                            page: false,
                            cellMinWidth: 100,
                            cols: [[]]
                        });
                    }
                }
            });
            return false;
        });

        form.on('submit(formUpdate)', function (data) {
            layer.load(2);
            var executeSql = $("[name=executeSql]").val();
            $("#myText").text("");
            $.ajax({
                url: "/api/AdminSql",
                type: 'put',
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify({ executeSql: executeSql }),
                dataType: "json",
                complete: function (obj) {
                    layer.closeAll('loading');
                },
                success: function (res) {
                    if (res.success) {
                        $("#myText").text(res.message);
                    } else {
                        $("#myText").text(res.message);
                    }
                    table.render({
                        elem: '#tableTbBas',
                        data: [],
                        page: false,
                        cellMinWidth: 100,
                        cols: [[]]
                    });
                }
            });
            return false;
        });
    });
</script>

